import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.*;

public class test1 {
    private JPanel jp;
    private JTextField textField1;
    private JTextField textField2;
    private JButton loginbtn;
    private JButton registerbtn;
    private JFrame jf = new JFrame("音乐播放器 - 登录界面");

    public test1() {
        jf.setSize(500, 500);
        jf.setLocationRelativeTo(null);
        jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        initComponents();
        addsj();

        jf.add(jp);
        jf.setVisible(true);
        registerbtn.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                new test2();
                jf.setVisible(false);
            }
        });
    }
    private void initComponents() {
        jp = new JPanel();
        textField1 = new JTextField(20);
        textField2 = new JPasswordField(20);
        loginbtn = new JButton("登录");
        registerbtn = new JButton("注册");
        jp.setLayout(null); // 设置面板布局为网格布局3
        JLabel j1=new JLabel("用户名:");
        JLabel j2=new JLabel("密码:");
        j1.setBounds(120,100,100,30);
        j2.setBounds(120,150,100,30);
        textField1.setBounds(180,100,200,30);
        textField2.setBounds(180,150,200,30);
        registerbtn.setBounds(180,300,200,30);
        loginbtn.setBounds(180,250,200,30);

        jp.add(j1);
        jp.add(textField1);
        jp.add(j2);
        jp.add(textField2);
        jp.add(loginbtn);
        jp.add(registerbtn);
    }
    public void addsj() {
        loginbtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 获取输入框输入的数据
                String username = textField1.getText();
                String password = textField2.getText();

                // 验证用户名和密码
                if (validateUser(username, password)) {
                    JOptionPane.showMessageDialog(jf, "登录成功", "登录信息", JOptionPane.INFORMATION_MESSAGE);
                    test3 t3 = new test3(username); // 传递用户名给 test3
                    jf.setVisible(false);
                } else {
                    JOptionPane.showMessageDialog(jf, "登录失败，账号或密码错误。", "错误信息", JOptionPane.ERROR_MESSAGE);
                }
            }
        });
    }

    // 用户验证方法
    private boolean validateUser(String username, String password) {
        String selectSQL = "SELECT * FROM users WHERE username = ? AND password = ?";
        try (Connection conn = DriverManager.getConnection(
                "jdbc:mysql://127.0.0.1:3306/mysql", "root", "190825DLdl"); // 替换为实际的数据库URL和登录凭证
             PreparedStatement pstmt = conn.prepareStatement(selectSQL)) {

            pstmt.setString(1, username);
            pstmt.setString(2, password);

            ResultSet resultSet = pstmt.executeQuery();
            return resultSet.next();
        } catch (SQLException e) {
            System.out.println("数据库错误: " + e.getMessage());
            return false;
        }
    }

    public static void main(String[] args) {
        new test1();
    }
}